package com.jy.project.service.imp;

import com.github.pagehelper.PageHelper;
import com.jy.project.domain.User;
import com.jy.project.dto.CreateAdminDTO;
import com.jy.project.dto.UserDTO;
import com.jy.project.dto.UserQueryDTO;
import com.jy.project.dto.UserUpdateDTO;
import com.jy.project.ex.BizException;
import com.jy.project.mapper.UserMapper;
import com.jy.project.service.DictService;
import com.jy.project.service.UserService;
import com.jy.project.vo.DictVO;
import com.jy.project.vo.UserVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DictService dictService;

    @Override
    public boolean insertUserDTO(UserDTO dto) {

        if (StringUtils.isEmpty(dto.getUserName())){
            throw new BizException("账户名称不能为空");
        }

        if (StringUtils.isEmpty(dto.getPwd())){
            throw new BizException("账户密码不能为空");
        }
        if (dto.getPwd().length()<6){
            throw new BizException("账户密码不能小于6位数");
        }

        if (dto.getPwd().length()>32){
            throw new BizException("账户密码太长了");
        }
        if(StringUtils.isNotEmpty(dto.getPhone())){
            if (dto.getPhone().length()!=11){
                throw new BizException("手机号码位数不对");
            }
        }
        if (StringUtils.isEmpty(dto.getNickName())){
            throw new BizException("昵称不能为空");
        }
        if (StringUtils.isEmpty(dto.getDeptNo())){
            throw new BizException("部门编号不能为空");
        }
        DictVO dictVO = dictService.getDictVO(dto.getDeptNo());
        if (dictVO==null){
            throw new BizException("找不到该部门编号");
        }
        dto.setDeptName(dictVO.getName());

        if (StringUtils.isEmpty(dto.getCompanyNo())){
            throw new BizException("公司编号不能为空");
        }
        DictVO dictCompanyVO = dictService.getDictVO(dto.getCompanyNo());
        if (dictCompanyVO==null){
            throw new BizException("找不到该公司编号");
        }
        dto.setCompanyName(dictCompanyVO.getName());
        User user=new User();
        BeanUtils.copyProperties(dto,user);
        user.setStatus(1);
        return this.insertUser(user);
    }

    @Override
    public List<UserVO> listUserVO(UserQueryDTO dto) {
        if (dto.getPageNum()!=null&& dto.getPageSize()!=null) {
            PageHelper.startPage(dto);
        }
        return userMapper.listUserVO(dto);
    }

    @Override
    public boolean updateUser(UserUpdateDTO dto) {
        if (StringUtils.isEmpty(dto.getPwd())){
            throw new BizException("账户密码不能为空");
        }
        if (dto.getPwd().length()<6){
            throw new BizException("账户密码不能小于6位数");
        }
        if (dto.getPwd().length()>32){
            throw new BizException("账户密码太长了");
        }
        if(StringUtils.isNotEmpty(dto.getPhone())){
            if (dto.getPhone().length()!=11){
                throw new BizException("手机号码位数不对");
            }
        }
        User user = new User();
        BeanUtils.copyProperties(dto,user);
        Date date = new Date();
        user.setUpdateAt(date);
        user.setIsAdmin(null);
        user.setStatus(null);
        return userMapper.updateByPrimaryKeySelective(user)>0;
    }

    @Override
    public boolean deleteUser(Long id) {
        if (id==null){
            throw new BizException("id 不能为空");
        }
        User user=new User();
        user.setUpdateAt(new Date());
        user.setUpdateDay(new Date());
        user.setIsDelete(1);
        Example example =new Example(User.class);
        example.createCriteria().andEqualTo("id",id);
        return userMapper.updateByPrimaryKeySelective(user)>0;
    }

    private boolean insertUser(User user){
        if (null==user){
            throw new BizException("用户对象不能为空");
        }
        String userName = user.getUserName();
        User u = this.getUser(userName);
        if (u!=null){
            throw new BizException("该账户已经注册");
        }
        user.setIsAdmin(0);
        user.setStatus(0);
        user.setIsDelete(0);
        Date date = new Date();
        user.setCreateAt(date);
        user.setUpdateAt(date);
        user.setUpdateDay(date);
       return  userMapper.insertSelective(user)>0;
    }
    @Override
    public User getUser(String userName){
        Example example=new Example(User.class);
        example.createCriteria().andEqualTo("userName",userName);
        User user = userMapper.selectOneByExample(example);
        return  user;
    }

    @Override
    @Transactional
    public boolean createAdminDTO(CreateAdminDTO dto) {
        Long userId = dto.getId();
        if(userId==null){
            throw  new BizException("授权用户id不能为空");
        }
        Example example=new Example(User.class);
        example.setForUpdate(true);
        User u = userMapper.selectByPrimaryKey(userId);
        if (u==null){
            throw  new BizException("找不到用户记录");
        }
        if (dto.getIsAdmin()==null){
            throw  new BizException("角色不能为空");
        }
        if (dto.getIsAdmin()==0){
            User update=new User();
            update.setId(userId);
            update.setIsAdmin(0);
            int size = userMapper.updateByPrimaryKeySelective(update);
            return  size>0;
        }
        example.createCriteria()
                .andEqualTo("isAdmin",1)
                .andEqualTo("deptNo",u.getDeptNo());
        User user = userMapper.selectOneByExample(example);
        if (user!=null){
            throw  new BizException("该部门已经有admin");
        }
        User update=new User();
        update.setId(userId);
        update.setIsAdmin(1);
        int size = userMapper.updateByPrimaryKeySelective(update);
        return size>0;
    }

    @Override
    public boolean adopt(Long id) {
        User user = new User();
        Date date = new Date();
        user.setId(id);
        user.setUpdateAt(date);
        user.setStatus(1);
        return userMapper.updateByPrimaryKeySelective(user)>0;
    }

    @Override
    public boolean reject(Long id) {
        User user = new User();
        Date date = new Date();
        user.setId(id);
        user.setUpdateAt(date);
        user.setStatus(2);
        return userMapper.updateByPrimaryKeySelective(user)>0;
    }

    @Override
    public List<User> listUser(UserQueryDTO dto) {
        Example example=new Example(User.class);
        example.createCriteria()
                .andLike("nickName","%"+dto.getNickName()+"%")
                .andNotEqualTo("id",dto.getUserId());
        List<User> users = userMapper.selectByExample(example);
        return users;
    }
}
